package com.bigegg.employee.service.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bigegg.employee.mapper.CollegeMapper;
import com.bigegg.employee.domain.College;
import com.bigegg.employee.service.ICollegeService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学院Service业务层处理
 * 
 * @author 刘博
 * @date 2022-08-11
 */
@Service
public class CollegeServiceImpl implements ICollegeService 
{
    public final Logger log = LoggerFactory.getLogger(CollegeServiceImpl.class);

    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 查询学院
     * 
     * @param id 学院主键
     * @return 学院
     */
    @Override
    public College selectCollegeById(Long id)
    {
        return collegeMapper.selectCollegeById(id);
    }

    /**
     * 查询学院列表
     * 
     * @param college 学院
     * @return 学院
     */
    @Override
    public List<College> selectCollegeList(College college)
    {
        return collegeMapper.selectCollegeList(college);
    }

    /**
     * 新增学院
     * 
     * @param college 学院
     * @return 结果
     */
    @Override
    public int insertCollege(College college)
    {
        college.setCreateTime(DateUtils.getNowDate());
        return collegeMapper.insertCollege(college);
    }

    /**
     * 修改学院
     * 
     * @param college 学院
     * @return 结果
     */
    @Override
    public int updateCollege(College college)
    {
        return collegeMapper.updateCollege(college);
    }

    /**
     * 批量删除学院
     * 
     * @param ids 需要删除的学院主键
     * @return 结果
     */
    @Override
    public int deleteCollegeByIds(Long[] ids)
    {
        return collegeMapper.deleteCollegeByIds(ids);
    }

    /**
     * 删除学院信息
     * 
     * @param id 学院主键
     * @return 结果
     */
    @Override
    public int deleteCollegeById(Long id)
    {
        return collegeMapper.deleteCollegeById(id);
    }

    /**
     * 上传excel
     */
    @Override
    public void importData(MultipartFile multipartFileile){

        InputStream is = null;
        try {
            is = multipartFileile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<College> list = EasyExcel.read(new BufferedInputStream(is)).head(College.class).sheet().headRowNumber(1).doReadSync();

        if(list!=null && list.size()>0){

            Long cnt = 0L;

            for(College college : list){

                // 存法律
                College c = new College();
                BeanUtils.copyProperties(college,c);

                c.setName(college.getName());

                List<College> collegeList = this.collegeMapper.selectCollegeList(c);

                if(collegeList==null || collegeList.size()==0){

                    college.setSort(cnt);
                    college.setIsDel(0L);
                    college.setCreateTime(new Date());

                    this.collegeMapper.insertCollege(college);

                }else {

                    this.log.info("此条已存在，无需新增，进行关联");
                }

                cnt++;
                this.log.info(String.format("已完成%d/%d",cnt,list.size()));
            }

        }
    }
}
